package com.groupbuying.settleproducer.service.impl;

import com.groupbuying.settleproducer.connector.FinancialFeign;
import com.groupbuying.settleproducer.constants.SettleUshopWithdrawalYsIntegralPaymentEnumConstants;
import com.groupbuying.settleproducer.dao.SettleUshopIntegralPaymentDao;
import com.groupbuying.settleproducer.domain.SettleUshopFlowCurrencyDO;
import com.groupbuying.settleproducer.domain.SettleUshopWithdrawalYsIntegralPaymentDO;
import com.groupbuying.settleproducer.service.SettleUshopFlowCurrencyService;
import com.groupbuying.settleproducer.service.SettleUshopIntegralPaymentService;
import com.groupbuying.settleproducer.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Service
public class SettleUshopIntegralPaymentServiceImpl implements SettleUshopIntegralPaymentService {
	public static final Logger logger = LoggerFactory.getLogger(SettleUshopIntegralPaymentServiceImpl.class);
	@Autowired
	private FinancialFeign financialFeign;
	@Autowired
	private SettleUshopIntegralPaymentDao settleUshopIntegralPaymentDao;
	@Autowired
	private SettleUshopFlowCurrencyService settleUshopFlowCurrencyService;
	
	@Override
	public SettleUshopWithdrawalYsIntegralPaymentDO get(Integer id){
		return settleUshopIntegralPaymentDao.get(id);
	}
	
	@Override
	public List<SettleUshopWithdrawalYsIntegralPaymentDO> list(Map<String, Object> map){
		return settleUshopIntegralPaymentDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return settleUshopIntegralPaymentDao.count(map);
	}
	
	@Override
	public int save(SettleUshopWithdrawalYsIntegralPaymentDO settleUshopWithdrawalYsIntegralPayment){
		return settleUshopIntegralPaymentDao.save(settleUshopWithdrawalYsIntegralPayment);
	}

	@Override
	public int update(SettleUshopWithdrawalYsIntegralPaymentDO settleUshopWithdrawalYsIntegralPayment){
		return settleUshopIntegralPaymentDao.update(settleUshopWithdrawalYsIntegralPayment);
	}

	@Override
	public int remove(Integer id){
		return settleUshopIntegralPaymentDao.remove(id);
	}

	@Override
	public int batchRemove(Integer[] ids){
		return settleUshopIntegralPaymentDao.batchRemove(ids);
	}
	/**
	 * 银盛积分代付
	 */
	@Override
	public Boolean createIntegralPayment(Map<String, Object> map) {
		Map<String,Object> financialIntegralMap =new HashMap<>();
		/*获取参数调用金融中心*/
		logger.info("SettleUshopIntegralPaymentServiceImpl.createIntegralPayment()-----积分代付从商户中心获取到的map信息:" + map.toString());
		// 解析map参数---商户号（门店id）
		Object tempParam = map.get("storeUuid");
		String storeUuid = tempParam == null ? null : tempParam.toString();
		//获取代付id（从商户中心获取）
		tempParam = map.get("withdrawalId");
		String withdrawalId = tempParam == null ? null : tempParam.toString();
		/*生成随机数4位*/
		String mathString = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
		String outTradeNo = DateUtils.format(new Date(), "yyyyMMdd") + storeUuid + mathString;
		// 解析map参数---订单说明
		tempParam = map.get("subject");
		String subject = tempParam == null ? null : tempParam.toString();
		/*-----------获取该商户的银行卡信息---------*/
		// 解析map参数---银行名称
		tempParam = map.get("bankName");
		String bankName = tempParam == null ? null : tempParam.toString();
		// 解析map参数---开户行所在城市
		tempParam = map.get("cityName");
		String bankCity = tempParam == null ? null : tempParam.toString();
		// 解析map参数---银行帐号
		tempParam = map.get("bankAccountNo");
		String bankAccountNo = tempParam == null ? null : tempParam.toString();
		// 解析map参数---银行帐号用户名
		tempParam = map.get("bankAccountName");
		String bankAccountName = tempParam == null ? null : tempParam.toString();
		// 解析map参数---收款方银行账户类型corporate :对公账户;personal:对私账户
		tempParam = map.get("bankAccountType");
		String bankAccountType = tempParam == null ? null : tempParam.toString();
		// 解析map参数---支持卡类型debit:借记卡;credit:信用卡 unit:单位结算卡
		tempParam = map.get("bankCardType");
		String bankCardType = tempParam == null ? null : tempParam.toString();
		// 解析map参数---积分金额
		tempParam = map.get("integralAmount");
		String integralAmountStr = tempParam == null ? null : tempParam.toString();
		BigDecimal integralAmount = new BigDecimal(integralAmountStr);
		//判断请求参数不为空
		if (StringUtils.isEmpty(integralAmountStr) || StringUtils.isEmpty(withdrawalId) || StringUtils.isEmpty(outTradeNo) || StringUtils.isEmpty(subject) || StringUtils.isEmpty(bankName) || StringUtils.isEmpty(bankCity) ||
				StringUtils.isEmpty(bankAccountNo) || StringUtils.isEmpty(bankAccountName) || StringUtils.isEmpty(bankAccountType) || StringUtils.isEmpty(bankCardType)
		) {
			logger.info("SettleUshopIntegralPaymentServiceImpl.createIntegralPayment()-----从store中心获取的数据可能存在空数据:" + map.toString());
			return false;
		}
		/*组装参数请求金融中心*/
		financialIntegralMap.put("outTradeNo",outTradeNo);
		financialIntegralMap.put("integralAmount",integralAmountStr);
		financialIntegralMap.put("bankName",bankName);
		financialIntegralMap.put("subject",subject);
		financialIntegralMap.put("bankCity",bankCity);
		financialIntegralMap.put("bankAccountName",bankAccountName);
		financialIntegralMap.put("bankCardType",bankCardType);
		financialIntegralMap.put("bankAccountType",bankAccountType);
		financialIntegralMap.put("bankAccountNo",bankAccountNo);
		Boolean status = financialFeign.integralPayment(financialIntegralMap);
		/*积分记录子表初始化*/
		/*记录积分代付子表记录*/
		SettleUshopWithdrawalYsIntegralPaymentDO settleUshopWithdrawalYsIntegralPaymentDO =new SettleUshopWithdrawalYsIntegralPaymentDO();
		try {
			settleUshopWithdrawalYsIntegralPaymentDO.setGmtCreate(new Date());
			settleUshopWithdrawalYsIntegralPaymentDO.setGmtModified(new Date());
			settleUshopWithdrawalYsIntegralPaymentDO.setIsDelete(SettleUshopWithdrawalYsIntegralPaymentEnumConstants.ENUM_ISDELETE_UNDEL);
			settleUshopWithdrawalYsIntegralPaymentDO.setStoreId(storeUuid);
			settleUshopWithdrawalYsIntegralPaymentDO.setOutTradeNo(outTradeNo);
			settleUshopWithdrawalYsIntegralPaymentDO.setWithdrawalId(withdrawalId);
			settleUshopWithdrawalYsIntegralPaymentDO.setIntegralPrice(integralAmount);
			settleUshopWithdrawalYsIntegralPaymentDO.setSplitState(SettleUshopWithdrawalYsIntegralPaymentEnumConstants.ENUM_SPLITSTATE_FAIL);
			int res= settleUshopIntegralPaymentDao.save(settleUshopWithdrawalYsIntegralPaymentDO);
			if(status && res>0){
				/*更改积分记录子表为成功状态*/
				Map<String,Object> condMap =new HashMap<>();
				condMap.put("withdrawalId",withdrawalId);
				condMap.put("splitState",SettleUshopWithdrawalYsIntegralPaymentEnumConstants.ENUM_SPLITSTATE_SUC);
				settleUshopIntegralPaymentDao.updateStatus(condMap);
				logger.info("SettleUshopIntegralPaymentServiceImpl.createIntegralPayment()-----积分子表落库成功，返回提现总表记录");
				return true;
			}else{
				logger.info("SettleUshopIntegralPaymentServiceImpl.createIntegralPayment()-----积分子表初始化失败");
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}


